/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: mp_buffer.h
 *
 * Purpose: the data buffer for micro player
 *
 * Developer:
 *   wen.gu , 2018-09-03
 *
 * TODO: 
 *
 ***************************************************************************/
#ifndef __MP_BUFFER_H__
#define __MP_BUFFER_H__

/******************************************************************************
**    INCLUDES
******************************************************************************/
#include <functional>

#include "mp_media.h"

/******************************************************************************
**    MACROS
******************************************************************************/

/** binding member function of class */
#define MAKE_BUFFER_OBSERVER(method) \
        std::bind(&method, this, std::placeholders::_1)


#define MAKE_BUFFER_OBSERVER2(method, obj) \
        std::bind(&method, obj, std::placeholders::_1)



/** binding non member function(or free function) */
#define MAKE_BUFFER_OBSERVER3(method) \
            std::bind(&method, std::placeholders::_1)

/******************************************************************************
**    TYPE DEFINITIONS
******************************************************************************/

struct BufferInfo
{
    uint32_t id = 0; /**< identify number */
    eMediaCategory category = eMediaCategory::Unknown; /** media category */
    eFrameType frameType    = eFrameType::Unknown;

    /** for es data: es format(eg. h264, aac) 
     *  for video frame: color format(eg. i420, nv12)
     *  for audio frame: sample(pcm) fromat(eg. s16l)
     */
    eMediaFormat format = eMediaFormat::Unknown;
    uint32_t duration   = 0; /** frame continue duration */

    int64_t pts; /** Presentation timestamp(uint in millisecond(ms) ) */
    int64_t dts; /** Decoding timestamp(uint in millisecond(ms)) */

    /** detail information of video or audio */
    MPAudioInfo audio;
    MPVideoInfo video;
};

class MPBuffer;
typedef std::function<void(MPBuffer*)> MPSignalBufferReturned;

/******************************************************************************
**    CLASSES/FUNCTIONS DEFINITIONS
******************************************************************************/

class MP_CLASS MPBuffer
{
public:
    MPBuffer(uint32_t capacity);
    MPBuffer(void* data, uint32_t capacity);

    virtual void release();
protected:
    virtual ~MPBuffer();

public:
    virtual uint8_t *base();
    virtual uint8_t *data();
    virtual size_t capacity() const;
    virtual size_t size() const;
    virtual size_t offset() const;

    virtual void setRange(size_t offset, size_t size);

    virtual void setObserver(MPSignalBufferReturned observer);

    virtual BufferInfo& info(){ return mInfo; }
    virtual int64_t pts() const;
    virtual int64_t dts() const;
protected:
    void *mData = nullptr;
    size_t mCapacity = 0;
    size_t mRangeOffset = 0;
    size_t mRangeLength = 0;
    bool mOwnsData = false;
    MPSignalBufferReturned mObserver = nullptr;
    BufferInfo mInfo;
};


#endif /** !__MP_BUFFER_H__*/


 

